#define _CRT_SECURE_NO_WARNINGS 1

#include "stack.h"

void STInit(ST* ps) {
	ps->stack = (STDataType*)malloc(INIT*sizeof(STDataType));
	if (ps->stack == NULL) {
		perror(malloc);
		return;
	}
	ps->capacity = INIT;
	ps->top = 0;
}

void STDestroy(ST* ps) {
	free(ps->stack);
	ps->stack = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

void STPush(ST* ps, STDataType x) {
	assert(ps);

	if (ps->top == ps->capacity) {
		STDataType* tmp=(STDataType*)realloc(ps->stack,(ADD+ps->capacity) * sizeof(STDataType));
		if (tmp == NULL) {
			perror(realloc);
			return;
		}
		ps->stack = tmp;
		ps->capacity += ADD;
	}
	ps->stack[ps->top] = x;
	ps->top++;
}

void STPop(ST* ps) {
	assert(ps);
	assert(!STEmpty(ps));

	ps->top--;
}

int STSize(ST* ps) {
	assert(ps);

	return ps->top;
}

bool STEmpty(ST* ps) {
	assert(ps);

	return ps->top==0;
}

STDataType STTop(ST* ps) {
	assert(ps);
	assert(!STEmpty(ps));

	return ps->stack[ps->top - 1];
}